<?php

declare(strict_types=1);

namespace app\model;

// 引入框架内置类

use think\facade\Request;
use think\facade\Session;

/**
 * +----------------------------------------------------------------------
 * | 管理员日志模型
 * +----------------------------------------------------------------------
 */
class SysLog extends Base
{
    protected $schema = [
        "id" => "int",  // 编号
        "userid" => "int",  // 管理员
        "username" => "varchar",  // 用户名
        "url" => "varchar",  // 操作页面	
        "title" => "varchar",  // 日志标题
        "content" => "text",  // 日志内容
        "ip" => "varchar",  // 操作IP
        "user_agent" => "text",  // User-Agent
        "create_time" => "datetime",  // 创建时间
        "update_time" => "datetime",  // 更新时间
    ];

    // 获取列表
    public static function queryPage(int $pageSize = 0)
    {
        $list = self::order('id desc')->paginate($pageSize);
        
        return $list;
    }

    // 管理员日志记录
    public static function record()
    {
        // 入库信息
        $userid   = Session::get('adminuser.userid', 0);
        $username   = Session::get('adminuser.username', '');
        $url       = Request::url();
        $title     = '';
        $content   = Request::except(['s', '_pjax']); //s 变量为系统内置的变量，_pjax为js的变量，无记录的必要
        $ip        = Request::ip();
        $userAgent = Request::server('HTTP_USER_AGENT');

        // 标题处理
        $route = str_replace("sys.", "", strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', Request::controller()))) . '/' . strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', Request::action()));
        
        $active =  \app\model\SysMenu::where('url', $route)->find();

        $title =  $active != null ? $active['name'] : "";

        // 内容处理(过长的内容和涉及密码的内容不进行记录)
        if ($content) {
            foreach ($content as $k => $v) {
                if (is_string($v) && strlen($v) > 200 || stripos($k, 'password') !== false || stripos($k, 'newpassword') !== false || stripos($k, 'repassword') !== false) {
                    unset($content[$k]);
                }
            }
        }

        // 插入数据
        if (!empty($title)) {
            // 查询管理员上一条数据
            $result = self::where('userid', '=', $userid)
                ->order('id', 'desc')
                ->find();
            if ($result) {
                if ($result->url != $url) {
                    self::create([
                        'title'      => $title ? $title : '',
                        'username'   => $username,
                        'content'    => ! is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
                        'url'        => $url,
                        'userid'   => $userid,
                        'user_agent' => $userAgent,
                        'ip'         => $ip
                    ]);
                }
            } else {
                self::create([
                    'title'      => $title ? $title : '',
                    'username'   => $username,
                    'content'    => ! is_scalar($content) ? json_encode($content, JSON_UNESCAPED_UNICODE) : $content,
                    'url'        => $url,
                    'userid'   => $userid,
                    'user_agent' => $userAgent,
                    'ip'         => $ip
                ]);
            }
        }
    }
}
